//
//  BMSOpMergeEventBuilder.m
//  WDBMS
//
//  Created by YeJary on 2025/5/13.
//

//  BMSOpMergeEventBuilder.m
//  WDBMS

#import "BMSOpMergeEventBuilder.h"

@interface BMSOpMergeEventBuilder ()

@end

@implementation BMSOpMergeEventBuilder

+ (NSString *)buildMDataWithEvents:(NSArray<NSDictionary *> *)events {
    if (!events || events.count == 0) return @"[]";
    NSMutableArray *sanitized = [NSMutableArray arrayWithCapacity:events.count];
    for (NSDictionary *e in events) {
        NSMutableDictionary *event = [e mutableCopy];
        if (!event[@"t"]) {
            event[@"t"] = @((NSInteger)[NSDate date].timeIntervalSince1970);
        }
        [sanitized addObject:event];
    }
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sanitized options:0 error:&error];
    if (error || !jsonData) return @"[]";
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (NSDictionary *)event_userActionWithAct:(NSString *)act
                               description:(NSString *)desc
                                    tab1:(nullable NSString *)tab1
                                    tab2:(nullable NSString *)tab2
                                    tab3:(nullable NSString *)tab3
                                    tabN:(nullable NSDictionary<NSString *, NSString *> *)tabN {
    NSMutableDictionary *dict = [@{
        @"d_type": @"u_ac",
        @"act": act ?: @"",
        @"wds": desc ?: @"",
        @"t": @((NSInteger)[NSDate date].timeIntervalSince1970)
    } mutableCopy];

    if (tab1.length) dict[@"tab1"] = tab1;
    if (tab2.length) dict[@"tab2"] = tab2;
    if (tab3.length) dict[@"tab3"] = tab3;

    // 动态添加 tabN（如 tab4、tab5...）
    [tabN enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
        if ([key hasPrefix:@"tab"] && key.length > 3) {
            dict[key] = value;
        }
    }];

    return dict;
}

+ (NSDictionary *)event_adActionWithAct:(NSString *)act
                                   desc:(NSString *)desc
                                   tab1:(nullable NSString *)tab1
                                   tab2:(nullable NSString *)tab2
                                   tab3:(nullable NSNumber *)tab3
                                 adType:(nullable NSString *)adType {
    NSMutableDictionary *dict = [@{
        @"d_type": @"ad_ac",
        @"act": act ?: @"",
        @"wds": desc ?: @"",
        @"t": @((NSInteger)[NSDate date].timeIntervalSince1970)
    } mutableCopy];

    if (tab1) dict[@"tab1"] = tab1;
    if (tab2) dict[@"tab2"] = tab2;
    if (tab3) dict[@"tab3"] = tab3;
    if (adType) dict[@"ad_t"] = adType;

    return dict;
}

+ (NSDictionary *)event_iapSuccessWithProductId:(NSString *)pid
                                    productName:(NSString *)name
                                     amountCent:(NSInteger)amount
                                        orderId:(NSString *)orderId
                                    productType:(nullable NSString *)type {
    return @{
        @"d_type": @"iap",
        @"act": @"purchase_success",
        @"wds": @"购买成功",
        @"pdi": pid ?: @"",
        @"pdt": type ?: @"",
        @"pdn": name ?: @"",
        @"pamt": @(amount),
        @"odn": orderId ?: @"",
        @"t": @((NSInteger)[NSDate date].timeIntervalSince1970)
    };
}

+ (NSDictionary *)event_userInfoWithPlatform:(NSString *)platform
                                          os:(NSString *)os
                                        idfa:(nullable NSString *)idfa
                                        idfv:(nullable NSString *)idfv
                                        imei:(nullable NSString *)imei
                                  android_id:(nullable NSString *)androidId
                                         mac:(nullable NSString *)mac
                                        oaid:(nullable NSString *)oaid
                                          dm:(nullable NSString *)deviceModel
                                          ct:(nullable NSString *)countryCode
                                       ym_id:(nullable NSString *)ymId {
    NSMutableDictionary *dict = [@{
        @"d_type": @"u",
        @"platform": platform ?: @"",
        @"os": os ?: @"",
        @"t": @((NSInteger)[NSDate date].timeIntervalSince1970)
    } mutableCopy];

    if (idfa) dict[@"idfa"] = idfa;
    if (idfv) dict[@"idfv"] = idfv;
    if (imei) dict[@"imei"] = imei;
    if (androidId) dict[@"android_id"] = androidId;
    if (mac) dict[@"mac"] = mac;
    if (oaid) dict[@"oaid"] = oaid;
    if (deviceModel) dict[@"d_m"] = deviceModel;
    if (countryCode) dict[@"ct"] = countryCode;
    if (ymId) dict[@"ym_id"] = ymId;

    return dict;
}

+ (NSDictionary *)event_userOpWithAct:(NSString *)act description:(NSString *)desc {
    return @{
        @"d_type": @"u_op",
        @"act": act ?: @"",
        @"wds": desc ?: @"",
        @"t": @((NSInteger)[NSDate date].timeIntervalSince1970)
    };
}

@end
